package com.umeox.ota.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.WindowManager;

import androidx.lifecycle.ViewModelProviders;

import com.umeox.ota.NoticeViewBinding;
import com.umeox.ota.R;
import com.umeox.ota.mvvm.viewModel.NoticeViewModel;
import com.umeox.ota.ui.filepicker.utils.FragmentUtil;
import com.umeox.ota.ui.fragment.OtaNewVersionFragment;
import com.umeox.ota.ui.fragment.OtaResultFragment;

/**
 * @author fighter_lee
 * @date 2018/9/5
 */
public class NoticeActivity extends BaseActivity<NoticeViewBinding, NoticeViewModel> {

    public static final String NOTICE_TYPE = "notice_type";
    public static final int NOTICE_TYPE_SHOW_NEW_VERSION = 1;
    public static final int NOTICE_TYPE_SHOW_DOWNLOAD_FINISH = 2;
    public static final int NOTICE_TYPE_SHOW_WILL_INSTALL_FORCE = 3;
    public static final int NOTICE_TYPE_SHOW_INSTALL_SUCCESS = 4;

    @Override
    protected void genViewModel() {
        mViewModel = ViewModelProviders.of(this, mViewModelFactory).get(NoticeViewModel.class);
    }

    @Override
    protected int genLayoutId(Bundle savedInstanceState) {
        return R.layout.activity_notice;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mBinding.setViewmodel(mViewModel);
        //设置弹框的尺寸
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();
        android.view.WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.7);
        getWindow().setAttributes(p);

        Intent intent = getIntent();
        int noticeType = intent.getIntExtra(NOTICE_TYPE, -1);
//        int noticeType = NOTICE_TYPE_SHOW_SHOW_DOWNLOAD_FINISH;
        mViewModel.setNoticeType(noticeType);

        switch (noticeType) {
            case NOTICE_TYPE_SHOW_NEW_VERSION:
            case NOTICE_TYPE_SHOW_DOWNLOAD_FINISH:
            case NOTICE_TYPE_SHOW_WILL_INSTALL_FORCE:
                OtaNewVersionFragment otaNewVersionFragment = new OtaNewVersionFragment();
                FragmentUtil.addFragment(this, R.id.dialog_fl, otaNewVersionFragment);
                break;
            case NOTICE_TYPE_SHOW_INSTALL_SUCCESS:
                OtaResultFragment otaResultFragment = new OtaResultFragment();
                FragmentUtil.addFragment(this, R.id.dialog_fl, otaResultFragment);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected boolean observerLifeCycle() {
        return false;
    }
}